//var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.js');
//var qqmapsdk;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: 'unknown',
    userInformation: {},
    msg: '获取校验码',
    currentTime: 60,
    disabled: false,
    address: null,
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'userInformation': app.globalData.userInformation
    });

    if (undefined != options.item) {
      console.log(options);
      this.setData({
        item: options.item
      });
      switch(options.item){
        case 'height':
          wx.setNavigationBarTitle({
            title: '身高'
          });
          break;
        case 'email':
          wx.setNavigationBarTitle({
            title: '邮箱'
          })
          break;
        case 'alipay':
          wx.setNavigationBarTitle({
            title: '支付宝'
          })
          break;
        default:
          break;
      }
      
    } else {
      console.log(optioins);
    }

    // qqmapsdk = new QQMapWX({
    //   key: 'BPGBZ-TGYWV-OCFPH-UWISA-3TZ5Z-REBTI' // 必填
    // });
  },
  getLocation: function () {
    var _this = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            var address = addressRes.result.formatted_addresses.recommend;
            _this.setData({
              address: address
            });
          }
        })
      }
    })
  },
  sendMessage: function () {
    var _this = this;

    var interval = setInterval(function () {
      _this.data.currentTime--;

      _this.setData({
        disabled: true,
        msg: _this.data.currentTime + ' 秒'
      });
      if (_this.data.currentTime <= 0) {
        clearInterval(interval)
        _this.setData({
          msg: '重新发送',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //console.log(this.data.userInformation);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  heightSubmit: function (e) {
    console.log('height');
    var height = e.detail.value.height;
    this.setData({
      disabled: true
    });
    var data = { 'height': height };
    this.updateUser(data);
  },
  emailSubmit: function (e) {
    console.log('email');
    var email = e.detail.value.email;
    var pattern = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
    if (!pattern.test(email)) {
      wx.showToast({
        title: '邮箱有误',
        icon: 'success',
        duration: 2000
      })
      return;
    }
    this.setData({
      disabled: true
    });
    var data = { 'email': email };
    this.updateUser(data);
  },
  addressSubmit: function (e) {
    var address = e.detail.value.address;
    var data = { 'residencePlace': address };
    this.updateUser(data);
  },
  alipaySubmit: function (e) {
    console.log('alipay');
    var alipay = e.detail.value.alipay;
    var data = { 'aliPayId': alipay };
    this.setData({
      disabled: true
    });
    this.updateUser(data);
  },
  updateUser: function (data) {
    var _this = this;
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/updateWechatUser',
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/json',
        'token': app.globalData.token
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 'ok') {
          for (var k in data) {
            app.globalData.userInformation[k] = data[k];
          }
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            _this.setData({
              disabled: false
            });
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }else{
          _this.setData({
            disabled: false
          });
          app.responseCheck(_this, res.data, '/pages/user/userinformation/update/update')
        }
      },
      fail: function (e) {
        _this.setData({
          disabled: false
        });
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },
})